<?php

namespace app\controllers;

use app\models\ContactForm;
use app\models\LoginForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;

class SiteController extends Controller
{
    public $layout = false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class'     => AccessControl::className(),
                'only'      => ['logout'],
                'rules'     => [
                    [
                        'actions' => ['logout'],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class'           => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }
    public function actionSay()
    {
        echo 'hello yii2';
    }
    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }

    public function actionJobs()
    {
        $config = [
            'queue'   => Yii::$app->params['queue'],
            'logPath' => Yii::$app->params['queue']['logPath'],
            'topics'  => Yii::$app->params['queue']['topics'],
        ];

        $jobs = new \Kcloze\Jobs\Jobs($config);

        if (!$jobs->queue) {
            die("queue object is null\n");
        }
        //jobs的topic需要在配置文件里面定义，并且一次性注册进去
        $topics = $jobs->queue->getTopics();
        var_dump($topics);

        //uuid和jobAction必须得有
        for ($i = 0; $i < 100; $i++) {
            $topicName = 'Hello';
            $uuid      = $jobs->queue->uuid();
            $data      = [
                'uuid'   => $uuid, 'jobName' => $topicName, 'jobAction' => 'index',
                'params' => [
                    'title' => 'kcloze', 'time' => time(),
                ],
            ];
            $result = $jobs->queue->push($topicName, $data);
            var_dump($result);
            echo $uuid . " ok\n";
            //$result = $jobs->queue->pop($topicName);
            //var_dump($result);
        }
        $result = $jobs->queue->pop($topicName);
        var_dump($result);
    }
}
